package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.message.bean.chat.ChatMessage;
import com.bnyy.message.bean.chat.message_data.GoodsMessage;
import com.bnyy.message.bean.chat.message_data.NoticeGoodsMessage;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.function.Consumer;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/1/9
 **/
public class NoticeGoodsAdapter extends BaseNormalListAdapter<ChatMessage, NoticeGoodsAdapter.ViewHolder> {

    public NoticeGoodsAdapter(Context context, Consumer<ChatMessage> consumer) {
        super(context, consumer);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_message_notice_goods, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ChatMessage chatMessage = getData(position);
        NoticeGoodsMessage noticeGoodsMessage = (NoticeGoodsMessage) chatMessage.getMessageData();

        holder.tvTitle.setText(noticeGoodsMessage.getTitle());

        String goodsJson = noticeGoodsMessage.getGoodsJson();

        ArrayList<GoodsMessage> goodsMessages = new Gson().fromJson(goodsJson, new TypeToken<ArrayList<GoodsMessage>>() {
        }.getType());

        int size = goodsMessages.size();
        if (size > 0) {
            if (size == 1) {
                holder.llGoodsImgs.setVisibility(View.GONE);
                holder.llSingleGoods.setVisibility(View.VISIBLE);

                GoodsMessage goodsMessage = goodsMessages.get(0);
                holder.tvGoodsName.setText(goodsMessage.getName());
                GlideHelper.setRoundImage(mContext, goodsMessage.getImg(), holder.iv);
                if (TextUtils.isEmpty(noticeGoodsMessage.getTag_name())) {
                    holder.tvType.setVisibility(View.GONE);
                } else {
                    holder.tvType.setVisibility(View.VISIBLE);
                    holder.tvType.setText(noticeGoodsMessage.getTag_name());
                    holder.tvType.setTextColor(Color.parseColor(noticeGoodsMessage.getTag_color()));
                    GradientDrawable bg = new GradientDrawable();
                    bg.setColor(Color.parseColor(noticeGoodsMessage.getTag_bgcolor()));
                    bg.setCornerRadius(SizeUtils.dp2px(5));
                    holder.tvType.setBackground(bg);
                }

                if (TextUtils.isEmpty(noticeGoodsMessage.getUse_desc())) {
                    holder.tvDesc.setVisibility(View.GONE);
                } else {
                    holder.tvDesc.setVisibility(View.VISIBLE);
                    holder.tvDesc.setText(noticeGoodsMessage.getUse_desc());
                }
            } else {
                holder.llSingleGoods.setVisibility(View.GONE);
                holder.llGoodsImgs.removeAllViews();

                int lines = 1;
                int overCount = 0;
                if (size > 3) {
                    lines = size / 3;

                    if (lines > 3) {
                        lines = 3;
                    } else if (lines < 3) {
                        if (size % 3 > 0) {
                            lines = lines + 1;
                        }
                    }

                    if (size > 9) {
                        overCount = size - 9;
                        if (overCount > 99) {
                            overCount = 99;
                        }
                    }
                }
                for (int i = 0; i < lines; i++) {
                    LinearLayout linearLayout = new LinearLayout(mContext);
                    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams linesLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    linesLayoutParams.setMargins(0, 0, 0, SizeUtils.dp2px(12));
                    linearLayout.setLayoutParams(linesLayoutParams);

                    int span = 3;
                    if (i == lines - 1) {
                        int i1 = size - ((i + 1) * span);
                        if (i1 < 0) {
                            span += i1;
                        }
                    }

                    int w = ScreenUtils.getScreenWidth() / 4;
                    for (int j = 0; j < span; j++) {
                        ImageView iv = new ImageView(mContext);
                        GlideHelper.setRoundImage(mContext, goodsMessages.get((i * 3) + j).getImg(), iv);

                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(w, w);

                        if (j == 1) {
                            layoutParams.weight = 1;
                            LinearLayout llMiddle = new LinearLayout(mContext);
                            llMiddle.setGravity(Gravity.CENTER);
                            llMiddle.addView(iv, new LinearLayout.LayoutParams(w, w));
                            linearLayout.addView(llMiddle, layoutParams);
                        } else {
                            if (i == lines - 1 && overCount > 0 && j == span -1) {
                                RelativeLayout rlOverCount = new RelativeLayout(mContext);

                                RelativeLayout.LayoutParams overCountIvLayoutParams = new RelativeLayout.LayoutParams(w, w);
                                rlOverCount.addView(iv, overCountIvLayoutParams);

                                TextView tvOverCount = new TextView(mContext);
                                tvOverCount.setTextAppearance(R.style.NormalTextView);
                                tvOverCount.setText("+" + overCount);

                                RelativeLayout.LayoutParams overCountTvLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                                GradientDrawable bg = new GradientDrawable();
                                bg.setColor(Color.parseColor("#80000000"));
                                bg.setCornerRadius(SizeUtils.dp2px(10));
                                tvOverCount.setBackground(bg);
                                tvOverCount.setTextColor(Color.WHITE);
                                tvOverCount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                                tvOverCount.setGravity(Gravity.CENTER);
                                tvOverCount.getPaint().setFakeBoldText(true);
                                rlOverCount.addView(tvOverCount, overCountTvLayoutParams);

                                linearLayout.addView(rlOverCount, layoutParams);
                            } else {
                                linearLayout.addView(iv, layoutParams);
                            }
                        }
                    }

                    holder.llGoodsImgs.addView(linearLayout);
                }
            }
        }
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        TextView tvTitle;
        LinearLayout llSingleGoods;
        ImageView iv;
        TextView tvGoodsName;
        TextView tvType;
        TextView tvDesc;

        LinearLayout llGoodsImgs;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tvTitle = itemView.findViewById(R.id.tv_title);
            llSingleGoods = itemView.findViewById(R.id.ll_single_goods);
            iv = itemView.findViewById(R.id.iv);
            tvGoodsName = itemView.findViewById(R.id.tv_goods_name);
            tvType = itemView.findViewById(R.id.tv_type);
            tvDesc = itemView.findViewById(R.id.tv_desc);
            llGoodsImgs = itemView.findViewById(R.id.ll_goods_imgs);
        }
    }
}
